package ${packagePrefix}.${domainObjectName?lower_case}.app.converter;

import ${packagePrefix}.${domainObjectName?lower_case}.client.dto.excel.${domainObjectName}ExcelDTO;
import ${packagePrefix}.${domainObjectName?lower_case}.client.dto.${domainObjectName}DTO;
import ${packagePrefix}.${domainObjectName?lower_case}.domain.${domainObjectName};
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

<#if generateComments>
/**
 * ${domainObjectName}Excel数据转换器
 * <p>
 * 负责Excel导入导出数据与领域模型之间的相互转换
 * </p>
 *
 * @author Auto Generated
 */
</#if>
@Component
public class ${domainObjectName}ExcelConverter {

    /**
     * 将Excel导入数据转换为领域对象列表
     *
     * @param excelDTOList Excel数据列表
     * @return 领域对象列表
     */
    public List<${domainObjectName}> convertToEntityList(List<${domainObjectName}ExcelDTO> excelDTOList) {
        if (CollectionUtils.isEmpty(excelDTOList)) {
            return Collections.emptyList();
        }
        
        return excelDTOList.stream()
                .map(this::convertToEntity)
                .collect(Collectors.toList());
    }
    
    /**
     * 将Excel导入数据转换为领域对象
     *
     * @param excelDTO Excel数据对象
     * @return 领域对象
     */
    public ${domainObjectName} convertToEntity(${domainObjectName}ExcelDTO excelDTO) {
        if (excelDTO == null) {
            return null;
        }
        
        ${domainObjectName} entity = new ${domainObjectName}();
<#list fields as field>
    <#if field.name != "id" && field.name != "createdTime" && field.name != "updatedTime" && field.name != "createdBy" && field.name != "updatedBy" && field.name != "isDeleted">
        <#if field.javaType == "String">
        entity.set${field.name?cap_first}(excelDTO.get${field.name?cap_first}());
        <#elseif field.javaType == "Integer" || field.javaType == "Long" || field.javaType == "BigDecimal" || field.javaType == "Boolean" || field.javaType == "Date" || field.javaType == "LocalDate" || field.javaType == "LocalDateTime">
        entity.set${field.name?cap_first}(excelDTO.get${field.name?cap_first}());
        </#if>
    </#if>
</#list>
        
        return entity;
    }
    
    /**
     * 将领域对象列表转换为Excel导出数据列表
     *
     * @param entityList 领域对象列表
     * @return Excel数据列表
     */
    public List<${domainObjectName}ExcelDTO> convertToExcelDTOList(List<${domainObjectName}> entityList) {
        if (CollectionUtils.isEmpty(entityList)) {
            return Collections.emptyList();
        }
        
        List<${domainObjectName}ExcelDTO> result = new ArrayList<>(entityList.size());
        for (int i = 0; i < entityList.size(); i++) {
            ${domainObjectName}ExcelDTO excelDTO = convertToExcelDTO(entityList.get(i));
            excelDTO.setRowIndex(i + 1); // 设置序号从1开始
            result.add(excelDTO);
        }
        
        return result;
    }
    
    /**
     * 将DTO对象列表转换为Excel导出数据列表
     *
     * @param dtoList DTO对象列表
     * @return Excel数据列表
     */
    public List<${domainObjectName}ExcelDTO> convertFromDTOList(List<${domainObjectName}DTO> dtoList) {
        if (CollectionUtils.isEmpty(dtoList)) {
            return Collections.emptyList();
        }
        
        List<${domainObjectName}ExcelDTO> result = new ArrayList<>(dtoList.size());
        for (int i = 0; i < dtoList.size(); i++) {
            ${domainObjectName}ExcelDTO excelDTO = convertFromDTO(dtoList.get(i));
            excelDTO.setRowIndex(i + 1); // 设置序号从1开始
            result.add(excelDTO);
        }
        
        return result;
    }
    
    /**
     * 将领域对象转换为Excel导出数据
     *
     * @param entity 领域对象
     * @return Excel数据对象
     */
    public ${domainObjectName}ExcelDTO convertToExcelDTO(${domainObjectName} entity) {
        if (entity == null) {
            return null;
        }
        
        ${domainObjectName}ExcelDTO excelDTO = new ${domainObjectName}ExcelDTO();
<#list fields as field>
    <#if field.name != "id" && field.name != "createdTime" && field.name != "updatedTime" && field.name != "createdBy" && field.name != "updatedBy" && field.name != "isDeleted">
        <#if field.javaType == "String">
        excelDTO.set${field.name?cap_first}(entity.get${field.name?cap_first}());
        <#elseif field.javaType == "Integer" || field.javaType == "Long" || field.javaType == "BigDecimal" || field.javaType == "Boolean" || field.javaType == "Date" || field.javaType == "LocalDate" || field.javaType == "LocalDateTime">
        excelDTO.set${field.name?cap_first}(entity.get${field.name?cap_first}());
        </#if>
    </#if>
</#list>
        
        return excelDTO;
    }
    
    /**
     * 将DTO对象转换为Excel导出数据
     *
     * @param dto DTO对象
     * @return Excel数据对象
     */
    public ${domainObjectName}ExcelDTO convertFromDTO(${domainObjectName}DTO dto) {
        if (dto == null) {
            return null;
        }
        
        ${domainObjectName}ExcelDTO excelDTO = new ${domainObjectName}ExcelDTO();
<#list fields as field>
    <#if field.name != "id" && field.name != "createdTime" && field.name != "updatedTime" && field.name != "createdBy" && field.name != "updatedBy" && field.name != "isDeleted">
        <#if field.javaType == "String">
        excelDTO.set${field.name?cap_first}(dto.get${field.name?cap_first}());
        <#elseif field.javaType == "Integer" || field.javaType == "Long" || field.javaType == "BigDecimal" || field.javaType == "Boolean" || field.javaType == "Date" || field.javaType == "LocalDate" || field.javaType == "LocalDateTime">
        excelDTO.set${field.name?cap_first}(dto.get${field.name?cap_first}());
        </#if>
    </#if>
</#list>
        
        return excelDTO;
    }
} 